package com.sinven.action;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ActionSupport;
import com.sinven.entity.Channel;
import com.sinven.service.ChannelService;
import com.sinven.util.GetUtil;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@Controller
@Scope("prototype")
@ParentPackage("json-default")
public class ChannelAction extends ActionSupport{
    private Integer id;
    private String name;
    @Autowired
    @Qualifier("ChannelService")
    private ChannelService channelService;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public ChannelService getChannelService() {
        return channelService;
    }

    public void setChannelService(ChannelService channelService) {
        this.channelService = channelService;
    }
    @Action(value = "listChannel",results = {@Result(type = "json")})//返回的数据格式为json
    //查询所有类别
    public String listChannel() throws IOException {
        PrintWriter out= GetUtil.getOut();
        List<Channel> channels=channelService.listChannel();
        String jsonString= JSON.toJSONString(channels);
        out.print(jsonString);
        for (Channel ch:channels){
            System.out.println(ch);
        }
        return NONE;
    }
}
